import 'package:flutter/material.dart';

class CustomLoading extends StatelessWidget {
  final String? content;
  final BoxConstraints constraints;

  const CustomLoading({Key? key,
    this.content,
    this.constraints = const BoxConstraints(minWidth: 130, maxWidth: 130, minHeight: 50, maxHeight: 50,),
  }): super(key: key);

  @override
  Widget build(BuildContext context) {
    double loadingMaxWidth = MediaQuery.of(context).size.width * 2 / 3;
    double iconSize = 19.0;
    double textLeftPadding = 8.0;
    double outPadding = 10.0;
    String loadingText = content ?? '加载中 ...';
    // 获取实际文字长度
    TextPainter textPainter = TextPainter(
      textDirection: TextDirection.ltr,
      text: TextSpan(
          text: loadingText,
          style: const TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w600,
              color: Colors.white,
              decoration: TextDecoration.none)),
    )..layout(
        maxWidth: loadingMaxWidth - iconSize - textLeftPadding, minWidth: 0);
    double maxWidth =
        textPainter.width + iconSize + textLeftPadding + outPadding * 2;

    return Center(
      child: Container(
        padding: EdgeInsets.all(outPadding),
        constraints: BoxConstraints(maxWidth: maxWidth, minWidth: iconSize + textLeftPadding),
        height: 50,
        width: loadingMaxWidth,
        decoration: BoxDecoration(color: const Color(0xff222222), borderRadius: BorderRadius.circular(5)),
        child: Center(
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              SizedBox(
                height: iconSize,
                width: iconSize,
                child: const CircularProgressIndicator(
                  strokeWidth: 2.0,
                  valueColor: AlwaysStoppedAnimation(Colors.white),
                ),
              ),
              Expanded(
                child: Container(
                  margin: EdgeInsets.only(left: textLeftPadding),
                  child: Text(
                    loadingText,
                    maxLines: 1,
                    style: const TextStyle(
                        fontSize: 15,
                        fontWeight: FontWeight.w600,
                        color: Colors.white,
                        decoration: TextDecoration.none),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}